package com.yld.demo2.util.tooltips;

import lombok.Data;

import java.util.Objects;


/**
 * 前后台信息交互类
 */
@Data
@SuppressWarnings("all")
public class ResBean<T> {

    Integer code;

    String msg;

    T data;

    public ResBean(ResposeEnum resEnum){
        this.code = resEnum.getCode();
        this.msg = resEnum.getMsg();
        this.data = null;
    }

    public ResBean(ResposeEnum resEnum, T data){
        this.code = resEnum.getCode();
        this.msg = resEnum.getMsg();
        this.data = data;
    }

    /**
     * 数据不存在，一般用到删除，修改，添加时前置验证
     * @return
     */
    public static ResBean noData(){
        return new ResBean(ResposeEnum.NO_DATA);
    }

    /**
     * 添加  修改   删除 时出错错误
     * @return
     */
    public static ResBean FAIL_DML(){
        return new ResBean(ResposeEnum.FAIL_DML);
    }

    /**
     * 通用的成功操作封装，api成功以后向前台返回信息调用此方法
     * @return
     */
    public static ResBean success(){
        return new ResBean(ResposeEnum.SUCCESS);
    }

    /**
     * 操作成功以后，带参数向前返回信息
     * @param data
     * @return
     */
    public static ResBean success(Object data){
        if(Objects.isNull(data)){
            return new ResBean(ResposeEnum.SUCCESS_NO_DATA);
        }
        return new ResBean(ResposeEnum.SUCCESS,data);
    }

    /**
     * 操作成功，但是数据库里面没有数据，一般用来于查询没有返回数据的情况
     * @return
     */
    public static ResBean successNoData(){
        return new ResBean(ResposeEnum.SUCCESS_NO_DATA);
    }

    /**
     * 添加、修改时 购买数量或金额为0的情况
     * @return
     */
    public static ResBean numberOrMoneyNotZero(){
        return new ResBean(ResposeEnum.NUMBER_OR_MONEY_NOT_ZERO);
    }

    /**
     * 前台参数给定的 日期格式不符
     * @return
     */
    public static ResBean formatDoesNotMatch(){
        return new ResBean(ResposeEnum.Format_Does_Not_Match);
    }
}